const Koa = require("koa");
const app = new Koa();
const Router = require("koa-router");
const router = new Router();
const bodyParser = require("koa-bodyparser");
const JWT = require("jsonwebtoken");
const key = "2202A";
const { AlipaySdk } = require("alipay-sdk");

//引入登陆列表数据
let userList = require("./mock/userInfor.json");
//引入：商品，分类，订单数据
let { goodsList, SlideList, orderList,doctorList,inforList } = require("./mock/index");
//使用中间件
//解决post请求
app.use(bodyParser());
//使用路由
app.use(router.routes()).use(router.allowedMethods());
app.listen(8090, () => {
  console.log("8090端口服务已启动");
});

/****************************以下都是接口**************************/

//支付接口配置项
// 实例化客户端
const alipaySdk = new AlipaySdk({
  // 设置应用 ID
  appId: "9021000131615549",
  // 设置应用私钥
  privateKey:
    "MIIEogIBAAKCAQEAvprH1iXC0cewzEP1EWtp0fyouNExA98m7bNiL2Ev+WXRoxXUxdSOT4zhZJVsiIMuSbiGh3Lwd0gIVHklrAoNzthRmHszV1Mw/DQn7kCkhvo34MWq5YDQ+f1B5+Sbbl2hBo23vWy7GCPTbQZ268K9jAVJdXXlPciQtbd5fb4vMVAs2Jdh+inoL95OzJpbN/u7ya9bidv/98g2bz71ohpH3anKZdpzbuv9BDDbTwxuPot+s88jF0yUQvgVbkVOKmsTzY4wyUz1dwDCeNinXNKI1DZ5Uo6bZ9uSe88QKxvKE7+cULJyBZzI6MlePNkBeXlcTMejsjW0aa61hqOs5WtyqQIDAQABAoIBAFLP+Ocn/N8cPcph9EYfPxjHB59U9FDUUZuRBqXDOg7HmC+dOpbBZRtXAE6tRTyCkkfhbkHHRIbuuv/Iqv92Vs152um/DW1xbaUGHBs95r3mVQTJnRidwS38RPg1sPpgn6ezRxN7/EXeuutxlZWVRLljSqlIEdOoIodxMc3aJbLse6lGRCL4txqTOwcq5OerhWQ2JpbYofoN7xfu/va0pZUViza7X8figTlSBfBROsgTMMI7jOiWJJubM1QOwLSgwq20qiq3Y5Ib/rld4+LnoNedxJPcZRY3xIrcHqNJF/BwSha4o83xq1/15bnresOMw/LdVtdfFE7Ejg0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ryliwvQYUU/7+RtBS4FCkhZocKNbIJG4QnFDcpaMrSqrkbx6AYoFBTbyBuJC62ittnq/WXH+o0HEtfJADd5+c7L6tSYM3qqbk/YE76oYgR0d7MJQp7DMXRcLGnhIdwL4SjxypME9ycewVS0T5NQprHtA7CEQ3JdFr9X5GjHm5dHAO9bkGb474rptCohCVrly+5hGI7yoFvpFy/fbFGfvHKWl1BJKRKmf0vQIDAQAB",
  //设置网关地址
  gateway: "https://openapi-sandbox.dl.alipaydev.com/gateway.do",
});

router.get("/pay", async (ctx) => {
  const { title, price, id } = ctx.request.query;
  const result = await alipaySdk.pageExec("alipay.trade.wap.pay", {
    method: "GET",
    bizContent: {
      out_trade_no: id,
      subject: title,
      total_amount: price,
    },
    returnUrl: "http://localhost:3000/payres",
  });

  ctx.body = {
    code: 200,
    data: result,
  };
});

//分类接口
router.get("/catelist", (ctx) => {
  ctx.body = {
    code: 200,
    SlideList,
  };
});

router.get("/orderlist", (ctx) => {
  ctx.body = {
    code: 200,
    orderList,
  };
});

//商品详情接口
router.get("/detail", (ctx) => {
  const { id } = ctx.request.query;
  const item = goodsList.find((item) => item.id === id);
  if (item) {
    ctx.body = {
      code: 200,
      msg: "数据获取成功",
      item,
    };
  } else {
    ctx.body = {
      code: 400,
      msg: "数据获取失败",
    };
  }
});

//商品列表接口
router.get("/goodslist", (ctx) => {
  ctx.body = {
    code: 200,
    msg: "success",
    goodsList,
  };
});
// 医生
router.get("/doctorlist", (ctx) => {
  ctx.body = {
    code: 200,
    msg: "success",
    doctorList,
  };
});

// 资讯详情页数据
router.get("/inforlist", (ctx) => {
  ctx.body = {
    code: 200,
    msg: "success",
    inforList,
  };
});

//登陆接口
router.post("/login", (ctx) => {
  //接受post方式传递的参数
  const { username, password } = ctx.request.body;
  const item = userList.find((item) => item.username === username);
  if (item) {
    //检测密码
    if (item.pasd === password) {
      //拷贝一份数据
      const dataInfor = { ...item };
      //删除数据中的密码项
      delete dataInfor.pasd;

      ctx.body = {
        code: 200,
        msg: "登陆成功",
        userinfor: dataInfor,
        token: JWT.sign(dataInfor, key),
      };
    } else {
      ctx.body = {
        code: 400,
        msg: "密码输入有误请重新输入",
      };
    }
  } else {
    ctx.body = {
      code: 400,
      msg: "请先注册再登陆",
    };
  }
});
